/**
 * @author Jossue
 */
//

var GroupButtonBuilder = Class.create();
GroupButtonBuilder.prototype = {
	// store buttons all in thi collection 
	all : null,
	
	//store active button
	active : null,
	
	//constructor
	initialize: function(arrayIds){
		this.all = Array();
		
		for(var x = 0;x<arrayIds.length;x++)
		{
			
			this.all.push(new Button(arrayIds[x],this));
		}
	},
	
	addButton: function(button){
		this.all.push(button);
		
	},
	
	makeAndAdd: function(id){
		this.all.push(new Button(id,this));
	}
}

var Button = Class.create();
Button.prototype = {
	path:null,
	file:null,
	ext:null,
	basefile:null,
	obj:null,
	offPosfix: null,
	onPosfix: null,
	overPosfix: null,
	status:null,
	notify:null,
	type:null,
	
	//constructor
	initialize: function(id, group){
		this.offPosfix = "off";
		this.onPosfix = "on";
		this.overPosfix = "over";
		
		this.obj = $(id);
		this.notify = group;
		
		if(group == null)
		{
			this.type = "single";
		}
		else
		{
			this.type = "group";
		}
		
		if(this.obj.src != null && this.obj.src != "")
		{
			this.path = this.obj.src.match(/.*\//);
			this.file = this.obj.src.replace(this.path,"");
			
			var punto = this.file.lastIndexOf(".");
			
			//get extencion
			this.ext = this.file.substring(punto, this.file.length);
			file2 = this.file.substring(0, punto);
			
			//get basefile
			var pattOn=new RegExp("^.*"+this.onPosfix+"$");
			var pattOff=new RegExp("^.*"+this.offPosfix+"$");
			var pattOver=new RegExp("^.*"+this.overPosfix+"$");
			
			 if(pattOn.test(file2))
			 {
			 	this.basefile = file2.substring(0,file2.lastIndexOf(this.onPosfix));
				this.status = this.onPosfix;
				
				if(group != null)
				{
					this.notify.active = this;
				}
			 }
			 else if(pattOff.test(file2))
			 {
			 	//alert(file2.lastIndexOf("off"))
			 	this.basefile = file2.substring(0,file2.lastIndexOf(this.offPosfix));
				this.status = this.offPosfix;
			 }
			 else if(pattOver.test(file2))
			 {
				this.basefile = file2.substring(0,file2.lastIndexOf(this.overPosfix));
				this.status = this.overPosfix;
			 }
			 else
			 {
			 	throw "bat file convention";
			 }
			//alert(this.basefile)
		}
		else
		{
			throw "Error! img not have src";
		}
		this.obj.observe('click', this.setPress.bind(this));
		this.obj.observe('mouseover', this.setOver.bind(this));
		this.obj.observe('mouseout', this.setOut.bind(this));
		
	},
	
	getPath: function(){
		return this.path;
	},
	
	setOver: function(){
		//refresh file path
		this.refreshPath();
		
		//change pointer
		this.obj.setStyle({
		  cursor:'pointer'
		});

		//change image
		this.file = this.basefile + this.overPosfix + this.ext;
		this.obj.src = this.path + this.file;
		
		
	},
	
	setOut: function(){
		//refresh file path
		this.refreshPath();
		
		//change pointer
		this.obj.setStyle({
		  cursor:'default'
		});
		
		//change image
		if(this.status == this.onPosfix)
		{
			this.file = this.basefile + this.onPosfix + this.ext;
		}
		else
		{
			this.file = this.basefile + this.offPosfix + this.ext;
		}
		
		this.obj.src = this.path + this.file;
		
	},
	
	setPress: function(){
		//refresh file path
		this.refreshPath();
		
		if(this.type == "group")
		{
			if(this.notify != null)
			{
				if(this.notify.active != null)
				{
					this.notify.active.setUnPress();
				}
				
				this.notify.active = this;
			}
			//change image
			this.file = this.basefile + this.onPosfix + this.ext;
			this.obj.src = this.path + this.file;
			this.status = this.onPosfix;

		}
		else
		{
			//change image
			this.file = this.basefile + this.onPosfix + this.ext;
			this.obj.src = this.path + this.file;
			this.status = this.offPosfix;
		}
	},
	
	setUnPress: function(){
		//refresh file path
		this.refreshPath();
		
		//change image
		this.file = this.basefile + this.offPosfix + this.ext;
		this.obj.src = this.path + this.file;
		this.status = this.offPosfix;
		
	},
	
	refreshPath: function(){
		this.path = this.obj.src.match(/.*\//);
		this.file = this.obj.src.replace(this.path,"")
	}
}